package com.jjh.back.servlet;

/**
 * 菜单的servlet
 * 授权的servlet
 * @author MyJJh
 */
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.servlet.BaseServlet;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.users.pojo.AMenu;
import com.jjh.users.pojo.ARole;
import com.jjh.users.pojo.ARoleMenu;
import com.jjh.users.service.IAuthDbService;
import com.jjh.users.service.IUsersDbService;
import com.jjh.users.service.impl.AuthDbServiceImpl;
import com.jjh.users.service.impl.UsersDbServiceImpl;

@Controller
@RequestMapping("/back/auth")

public class AuthBackServlet extends BaseServlet
{
	@Autowired
	private IAuthDbService authDbService ;
	@Autowired
	private IUsersDbService usersDbService ;
	
	

	/**
	 * 授权提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping(value = "roleMenuInsertSubmit",produces = "text/html;charset=UTF-8" )
	@ResponseBody
	private String roleMenuInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		JSONObject resultJson = new JSONObject();
		
		String roleId = request.getParameter("roleId");
		String menuIds = request.getParameter("menuIds");
		if(menuIds == null)
		{
			menuIds = "" ;
		}
		
		if(!"".equalsIgnoreCase(menuIds))
		{
			/* 先删除原来角色中所有的菜单 */
			Map<String,Object> condMap = new HashMap<String, Object>();
			
			condMap.put("roleId",roleId);
			this.authDbService.deleteOneRoleMenuService(condMap);
			
			/* 按照逗号进行拆分 */
			String[] menuIdss = menuIds.split(",");
			
			int totalCount = menuIdss.length;
			int succedCount = 0 ;
			int failedCount = 0 ;
			
			for(String idTemp : menuIdss)
			{
				/* 保存到数据库中 */
				ARoleMenu roleMenu = new ARoleMenu();
				
				roleMenu.setMenuId(Integer.valueOf(idTemp));
				roleMenu.setRoleId(Integer.parseInt(roleId));
				roleMenu.setStatus(Byte.valueOf("1"));
				roleMenu.setPubTime(new Date());
				
				JSONObject resultDbJson = this.authDbService.saveOneRoleMenuService(roleMenu);
				/* 0 = 0 */
				if(resultDbJson.get(InfoEnum.VAR_CODE.getInfo() + "").equals(InfoEnum.INFO_SUCCESS.getCode()))
				{
					/* 成功 */
					succedCount ++ ;
				}else
				{
					failedCount ++ ;
				}
			}
			
			resultJson.put("code", InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put("info", ConstatFinalUtil.INFO_JSON.get(InfoEnum.INFO_SUCCESS.getCode()));
			
			JSONObject dateResJson = new JSONObject();
			dateResJson.put("totalCount", totalCount);
			dateResJson.put("succedCount", succedCount);
			dateResJson.put("failedCount", failedCount);
			
			resultJson.put("data", dateResJson);
		}else
		{
			resultJson.put("code", "-9");
			resultJson.put("info", "请选择菜单");
		}
		
		System.out.println("====授权===" + resultJson.toString());
		
		return resultJson.toJSONString() ;
	}
	
	/**
	 * 查看树形结构 不能分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/menuTreeList")
	private String menuTreeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("==menuTreeList===");
		
		Map<String,Object> condMap = new HashMap<String, Object>();
		List<AMenu> menuList = this.authDbService.findCondListMenuService(null, condMap);
		/* 存储到四大作用域中 */
		request.setAttribute("list", menuList);
		
		
		String roleId = request.getParameter("roleId");
		if(roleId != null && !"".equalsIgnoreCase(roleId))
		{
			/* 根据id查询角色 */
			condMap.clear();
			condMap.put("id", roleId);
			ARole role = this.usersDbService.findOneRoleService(condMap);
			request.setAttribute("role", role);
			
			PageInfoUtil pageInfoUtil = proccedPageInfoUtil(request);
			/* 循环menuList */
			for (AMenu menuTemp : menuList)
			{
				condMap.clear();
				condMap.put("roleId", role.getId());
				condMap.put("menuId", menuTemp.getId());
				this.authDbService.findCondListRoleMenuService(pageInfoUtil, condMap);
				if(pageInfoUtil.getTotalRecord() > 0)
				{
					/* 说明查询到了 */
					menuTemp.setCheckStr("1");
				}
			}
			/* 授权页面 */
			return  "back/auth/menuTreeAuthList" ;
		}
		return "/back/auth/menuTreeList" ;
	}

	/**
	 * 菜单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/menuList")
	private String menuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("==menuList=");
		
		/* 分页工具类 */
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(request);
		
		/* 处理搜索条件 */
		Map<String,Object> condMap = proccedSearchCond(request);
		
		/*====上一级菜单====*/
		String parentId = request.getParameter("parentId");
		if(parentId == null)
		{
			parentId = "" ;
		}
		condMap.put("parentId", parentId);
		request.setAttribute("parentId", parentId);
		
		/* 查询关联关系 */
		condMap.put("extend", "true");
		List<AMenu> menuList = this.authDbService.findCondListMenuService(pageInfoUtil, condMap);
		/* 存储到四大作用域中 */
		request.setAttribute("list", menuList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/*
		 * 查询整棵树的树形结构
		 * 在管理员列表中查询所有的角色一样
		 */
		List<AMenu> menuTreeList = this.authDbService.findCondListTreeMenuService(0, null);
		/* 存储到四大作用域中 */
		request.setAttribute("menuTreeList", menuTreeList);
		/* 服务端跳转 */
		return "/back/auth/menuList" ;
	}
}
